<template>
    <div id="recommend">
          <div class="re_item" :key="i" v-for="(r,i) in jokes">
            <div class="header">
              <md-avatar class="re_avatar">
                <img :src="r.header" alt="People">
              </md-avatar>
              <div class="sub">
                <span>{{r.username}}</span>
                <span>{{r.passtime}}</span>
              </div>
            </div>
            <div class="contents">
              <div>{{r.text}}</div>
              <video-player v-if="r.video" class="video-player vjs-custom-skin"
                     ref="videoPlayer"
                     :playsinline="true"
                     :options="r.options"
                     @play="onPlayerPlay($event)"
                     @pause="onPlayerPause($event)">
              </video-player>
              <md-content v-else-if="r.gif" class="md-scrollbar imgs">
                <img :src="r.gif" alt="此照片无法显示！">
              </md-content>
              <md-content v-else class="md-scrollbar imgs">
                <img :src="r.image" alt="此照片无法显示！">
              </md-content>
            </div>
            <Comment :up="r.up" :down="r.down" :fenxiang="r.forward" :comment="r.comment"></Comment>
          </div>
    </div>
</template>
<script>
import { videoPlayer } from "vue-video-player";
import { mapActions, mapState } from "vuex";
import Comment from "@/components/Comment";
export default {
  name: "recommend",
  components: {
    videoPlayer,
    Comment
  },
  data() {
    return {
      playerOptions: {
        autoplay: false, //如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: "auto", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: "zh-CN",
        aspectRatio: "16:9", // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        poster: "", //封面地址
        width: document.documentElement.clientWidth,
        notSupportedMessage: "此视频暂无法播放，请稍后再试" //允许覆盖Video.js无法播放媒体源时显示的默认信息。
      }
    };
  },
  computed: {
    ...mapState({
      jokes: state => {
        return (
          state.jokes &&
          state.jokes.map(joke => {
            return {
              ...joke,
              options: {
                ...this.playerOptions,
                sources: [
                  {
                    src: joke.video,
                    type: "video/mp4"
                  }
                ]
              }
            };
          })
        );
      }
    })
  },
  mounted() {
    this.getJokes({ type: "1" });
  },
  methods: {
    ...mapActions({
      getJokes: "GET_JOKES"
    }),
    onPlayerPlay(player) {
      alert("play");
    },
    onPlayerPause(player) {
      alert("pause");
    }
  }
};
</script>
<style lang="less" scoped>
@import "../styles/var";
  .re_item {
    margin-bottom: 10px;
    .header {
      .md-avatar.re_avatar {
        width: 30px;
        min-width: 30px;
        height: 30px;
        margin-right: 8px;
        vertical-align: top;
        margin-top: 2px;
      }
      .sub {
        display: inline-block;
        span {
          display: block;
        }
        & > span:nth-child(1) {
          font-size: @smallSize;
          font-weight: bold;
        }
        & > span:nth-child(2) {
          font-size: @smallSize;
          color: @zong;
        }
      }
    }
    .contents {
      .imgs {
        max-height: 150px;
        height: 150px;
        overflow: auto;
      }
    }
  }
</style>
